package com.hyy.dao.daoImpl;

import com.hyy.bean.Cart;
import com.hyy.bean.MaskOrder;
import com.hyy.dao.OrderDao;
import com.hyy.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
	@Override
	public boolean insertOrder(MaskOrder order) throws SQLException {
		Connection conn = JdbcUtil.getConnection();
		String sql = "insert into mask_order(order_id,uid, recever_name, recever_phone, " +
				"receiver_province," +
				" receiver_city, receiver_district, recevier_address, receiver_zip,price, status)" +
				" " +
				"value (?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setString(1,order.getOrderId());
		pstmt.setLong(2,order.getUid());
		pstmt.setString(3,order.getReceverName());
		pstmt.setString(4,order.getReceverPhone());
		pstmt.setString(5,order.getReceiverProvince());
		pstmt.setString(6,order.getReceiverCity());
		pstmt.setString(7,order.getReceiverDistrict());
		pstmt.setString(8,order.getRecevierAddress());
		pstmt.setString(9,order.getReceiverZip());
		pstmt.setDouble(10,order.getPrice());
		pstmt.setLong(11,order.getStatus());

		int n = pstmt.executeUpdate();

		return n==1?true:false;
	}

	@Override
	public boolean deleteOrderByOid(Long oid) throws SQLException {
		return false;
	}

	@Override
	public boolean updateOrder(MaskOrder order) throws SQLException {
		return false;
	}

	@Override
	public List<MaskOrder> showAllByUid(long uid) throws SQLException {
		Connection conn = JdbcUtil.getConnection();
		String sql = "select * from mask_order where uid=?;";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setLong(1,uid);
		ResultSet rs = pstmt.executeQuery();
		List<MaskOrder> maskOrders = new ArrayList<>();
		while (rs.next()){
			maskOrders.add(new MaskOrder(rs.getString(1),rs.getLong(2),
					rs.getString(3),rs.getString(4),rs.getString(5),
					rs.getString(6),rs.getString(7),rs.getString(8),
					rs.getString(9),rs.getDouble(10),rs.getLong(11),
					rs.getTimestamp(11),rs.getTimestamp(12)));
		}
		return maskOrders;
	}
}
